import re


def read_and_wirte():
    write_re = re.compile("完成了一页 , 当前页码是:.*|当前页item_out_id_set 的 大小是.*|访问成功.*|已经是最后一页了准备进入下一搜索类型.*")
    read_log_file_path = "jx2.log"
    write_log_file_path = "newlog.log"
    with open(write_log_file_path, "at", encoding="utf-8") as f1:
        with open(read_log_file_path, 'rt', encoding="utf-8") as f:
            for line in f:
                line_str = re.search(write_re, line)
                if line_str:
                    f1.write(line_str.group())
                    f1.write("\n")


def get_text():
    read_log_file_path = "newlog.log"
    write_log_file_path = "new.log"
    with open(write_log_file_path, "at", encoding="utf-8") as f1:
        with open(read_log_file_path, 'rt', encoding="utf-8") as f:
            for line in f:
                if "访问成功" in line:
                    f1.write(f"搜索项目: {line.strip()[-1:]} ,")
                elif "完成了一页" in line:
                    current_page_num = re.sub("\D", "", line)
                    f1.write(f"当前页数是: {current_page_num} ,")
                elif "当前页" in line:
                    item_count = re.sub("\D", "", line)
                    f1.write(f"项目总数是: {item_count} \n")
                else:
                    print("出错了!")


def statistics_and_print_dict():
    count_dict = {}
    read_log_file_path = "new.log"
    with open(read_log_file_path, 'rt', encoding="utf-8") as f:
        for line in f:
            array = line.split(",")
            key = array[0].strip()[-1:]
            page_num = re.sub("\D", "", array[1])
            item_count = re.sub("\D", "", array[2])
            if key in count_dict.keys():
                count_dict[key][0] = page_num
                count_dict[key][1] = item_count
            else:
                list_i = [page_num, item_count]
                count_dict[key] = list_i

    print(count_dict)


if __name__ == '__main__':
    statistics_and_print_dict()
